#++++++++++++++++++++++++++++++++++++++++++++++++++++++++ #
#### R-Script for Replication of "Unraveling Proxy Wars" ####

#~~~~~~~~~~~ International Interactionns (2024) ~~~~~~~~~~#
# ++++++++++++++++++++++++++++++++++++++++++++++++++++++++#

R
# Author: Natalia Tellidou
# Date: October 15, 2024
# R version 4.2.3

# The dataset file for replicating the data is "unraveling_proxy_wars.csv"
# The QCA was conducted using the packages QCA (Dusa 2019) and SetMethods (Oana and Schneider 2018).



##### Preparation ####
#~~~~~~~~~~~~~~~~~~~~~

# Starting commands (working space & directory)
rm(list=ls()) 

setwd("~/Desktop/Revision International Interactions")

# Loading packages & data

library(SetMethods)
library (QCA)


proxy_war<- read.csv("unraveling_proxy_wars.csv", header = TRUE, sep = ";", dec = ".", row.names = 1) 


##### Description of the dataset ####
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

# Outcome: SUP = state sponsorship  (coding fuzzy set)

proxy_war$SUP <-calibrate(proxy_war$SUP, 
                         type = "fuzzy", 
                         method = "direct", 
                         thresholds = "0, 49, 100")

# Conditions (coding fuzzy set):

# RV = Rivalry
# TT = Transnational threat
# OP = Opportunity
# CA = Credible ally 

proxy_war$RV <-calibrate(proxy_war$RV, 
                      type = "fuzzy", 
                      method = "direct", 
                      thresholds = "0, 50, 100")

proxy_war$TT <-calibrate(proxy_war$TT, 
                         type = "fuzzy", 
                         method = "direct", 
                         thresholds = "0, 50, 100")

proxy_war$OP <-calibrate(proxy_war$OP, 
                         type = "fuzzy", 
                         method = "direct", 
                         thresholds = "0, 50, 100")

proxy_war$CA <-calibrate(proxy_war$CA, 
                         type = "fuzzy", 
                         method = "direct", 
                         thresholds = "0, 50, 100")




##### 1. Standard analysis: presence of outcome (state sponsorship) ####
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#

# 1.1. Necessity analysis for state sponsorship
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


# Single necessary conditions (see table 4 in the main text)
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

QCAfit(proxy_war[,1:4], 
       proxy_war$SUP, 
       necessity = TRUE, 
       neg.out = FALSE)

# result: none of the conditions is a necessary condition for state sponsorship 



# 1.2. Sufficiency analysis for state sponsorship 
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

# Constructing the truth table (see table 5 in the main text and the full table A7 in the appendix)
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


TT_proxywar<- truthTable(data = proxy_war, outcome = "SUP", conditions = c("OP", "TT", "RV", "CA"), incl.cut = 0.8, pri.cut = 0.51, n.cut = 1, sort.by = "incl", show.cases = TRUE)

TT_proxywar

stargazerTT(TT_proxywar, show.cases = TRUE, type ="text", out = "TT_proxywar.txt")

write.csv(TT_proxywar$tt, "TT_proxywar.csv") # Save the truth table element in a new file


# Logical minimization 
#~~~~~~~~~~~~~~~~~~~~~~

# Conservative solution (see table A8 in the appendix) 

sol_proxywar_cons <- minimize(TT_proxywar, 
                     include = "1", 
                     details = TRUE, 
                     use.tilde = TRUE)
sol_proxywar_cons

# Most parsimonious solution (see table A9 in the appendix)

sol_proxywar_pars <- minimize(TT_proxywar,
                                details = TRUE,
                                include = "?")
sol_proxywar_pars


# Check simplifying assumptions (not present in the text or the appendix, only for R)

sol_proxywar_pars$SA

# Intermediate solution (see Table 6 in the main text)

sol_proxywar_inter <- minimize(TT_proxywar, 
                         include = "?", 
                         details = TRUE, 
                         use.tilde = TRUE,
                         exclude = c(15),
                         dir.exp = c(~RV, CA))
sol_proxywar_inter

# Check easy counterfactuals, unobserved truth table rows, if they exist. 


sol_proxywar_inter$i.sol$C1P1$EC


# 1.3. Histograms of calibrated sets 
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

# Constructing the histograms for conditions and outcome (see Figure A1 in the appendix)
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

hist(proxy_war$SUP)
hist(proxy_war$RV)
hist(proxy_war$TT)
hist(proxy_war$OP)
hist(proxy_war$CA)

### 2.1 Robustness test: Conditions and outcome turned into crisp sets####
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

# Starting commands to recalibrate the data in order to run the robustness test
rm(list=ls()) 

# Loading data
proxy_war<- read.csv("unraveling_proxy_wars.csv", header = TRUE, sep = ";", dec = ".", row.names = 1) 

# Calibrating conditions and outcome as crisp sets
proxy_war$RV <-calibrate(proxy_war$RV, 
                      type = "crisp", 
                      method = "direct", 
                      thresholds = "50")

proxy_war$TT <-calibrate(proxy_war$TT, 
                      type = "crisp", 
                      method = "direct", 
                      thresholds = "50")

proxy_war$OP <-calibrate(proxy_war$OP, 
                         type = "crisp", 
                         method = "direct", 
                         thresholds = "50")

proxy_war$CA <-calibrate(proxy_war$CA, 
                      type = "crisp", 
                      method = "direct", 
                      thresholds = "50")

proxy_war$SUP <-calibrate(proxy_war$SUP, 
                       type = "crisp", 
                       method = "direct", 
                       thresholds = "50")

# Constructing the truth table (not reported in the appendix, but can be displayed in R
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

TT_rob1<- truthTable(data = proxy_war, outcome = "SUP", conditions = c("OP", "TT", "RV", "CA"), incl.cut = 0.8, pri.cut = 0.51, n.cut = 1, sort.by = "incl", show.cases = TRUE)

TT_rob1

# Intermediate solution (see Table A10 in the appendix)
# I replicate the intermediate solution as I report the intermediate solution for the presence of state sponsorship in the main paper.

sol_proxywar_rob1<- minimize(TT_rob1, 
                       include = "?", 
                       details = TRUE, 
                       use.tilde = TRUE,
                       exclude = c(15),
                       dir.exp = c(RV,~CA))
sol_proxywar_rob1

#### 2.2 Robustness test: alternative calibrations of the outcome state sponsorship####
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Starting commands to re calibrate the data in order to run the second robustness test
rm(list=ls()) 

# Loading data
proxy_war<- read.csv("unraveling_proxy_wars.csv", header = TRUE, sep = ";", dec = ".", row.names = 1) 

# Calibrating outcome with different thresholds
proxy_war$SUP <-calibrate(proxy_war$SUP, 
                         type = "fuzzy", 
                         method = "direct", 
                         thresholds = "0, 35, 100")

# Calibrating conditions
proxy_war$RV <-calibrate(proxy_war$RV, 
                         type = "fuzzy", 
                         method = "direct", 
                         thresholds = "0, 50, 100")

proxy_war$TT <-calibrate(proxy_war$TT, 
                         type = "fuzzy", 
                         method = "direct", 
                         thresholds = "0, 50, 100")

proxy_war$OP <-calibrate(proxy_war$OP, 
                         type = "fuzzy", 
                         method = "direct", 
                         thresholds = "0, 50, 100")

proxy_war$CA <-calibrate(proxy_war$CA, 
                         type = "fuzzy", 
                         method = "direct", 
                         thresholds = "0, 50, 100")

# Constructing the truth table (not reported in the appendix, but can be displayed in R)
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

TT_rob2<- truthTable(data = proxy_war, outcome = "SUP", conditions = c("OP", "TT", "RV", "CA"), incl.cut = 0.8, pri.cut = 0.51, n.cut = 1, sort.by = "incl", show.cases = TRUE)
TT_rob2

# Intermediate solution (see Table A11 in the appendix)
# I replicate the intermediate solution as I report the intermediate solution for the presence of state sponsorship in the main paper.

sol_proxywar_rob2 <- minimize(TT_rob2, 
                         include = "?", 
                         details = TRUE, 
                         use.tilde = TRUE,
                         dir.exp = c(~RV,CA))

sol_proxywar_rob2

# end of script









